import type { Menu } from '@/typings/menu'

/**
 * 将权限路由转换成搜索的菜单数据
 * @param routes - 权限路由
 * @param treeMap
 */
export function transformSearchMenus(routes: Menu[], treeMap: Menu[] = []) {
    if (routes && routes.length === 0) return []
    return routes.reduce((acc, cur) => {
        if (cur.items && cur.items.length > 0) {
            transformSearchMenus(cur.items, treeMap)
        } else if (cur.url) {
            acc.push(cur)
        }
        return acc
    }, treeMap)
}

/** 将路由名字转换成路由路径 */
export function transformRouteNameToRoutePath(
    name: Exclude<AuthRoute.AllRouteKey, 'not-found'>
): AuthRoute.RoutePath {
    const rootPath: AuthRoute.RoutePath = '/'
    if (name === 'root') return rootPath

    const splitMark = '_'
    const pathSplitMark = '/'
    const path = name.split(splitMark).join(pathSplitMark)

    return (pathSplitMark + path) as AuthRoute.RoutePath
}
